#ifndef AS5047P_H
#define AS5047P_H

#include <stdint.h>
#include <stdbool.h>
#include <math.h>

#include "spi.h"
#include "gpio.h"


#define AS5047P_2PI 6.28318530718
#define ASP5047_CPR 16384

#define CMD_ANGLECOM       0x3FFF  
#define CMD_READ_MAG       0x3FFE 
#define CMD_READ_DIAG      0x3FFD  
#define CMD_NOP            0x0000  
#define CMD_CLEAR_ERROR    0x0001  
#define CMD_ProgramControl 0x0003
#define CMD_OTPHigh 0x0016
#define CMD_OTPLow 0x0017


typedef struct as5047p_stru{

	SPI_HandleTypeDef * hspi;//spi句柄

	GPIO_TypeDef * GPIO_PORT_CS;//cs引脚

	uint16_t GPIO_PIN_CS;//cs引脚

	uint8_t error_flag;//error flag

	uint16_t encoder_data;//new angle data
	
	
	uint16_t cpr;//每转脉冲数（CPR）
	
	uint16_t encoder_data_prev;//new angle data
	
	float d_encoder;//编码值差
	
	float full_rotation_offset;
	
	float total_angle;// 多圈角度值 = （满旋转次数）*2PI + 当前传感器角度
	float only_angle;//单圈角度值
	float total_angle_radian;//多圈弧度值
	float only_angle_radian;//单圈弧度值

}as5047p_t;



extern uint8_t as5047p_get_data(as5047p_t* obj);
extern uint8_t as5047p_read_encoder(as5047p_t* obj);//read angle data from as5047p
extern uint8_t as5047p_obj_create(as5047p_t* obj,SPI_HandleTypeDef * hspi,GPIO_TypeDef * GPIO_PORT_CS,uint16_t GPIO_PIN_CS);

#endif
